<?php
/*
Simple:Press Forum
Login (etc) Form Actions and Filters
$LastChangedDate: 2009-03-03 10:13:32 +0000 (Tue, 03 Mar 2009) $
$Rev: 1511 $
*/

include_once(SF_PLUGIN_DIR.'/forum/sf-primitives.php');

function sf_login_header() 
{
	if(is_admin()) return;

	$sflogin=array();
	$sflogin=get_option('sflogin');
	if(!$sflogin['sfloginskin']) return;

	if(isset($_REQUEST['view'])) 
	{
		$sfstyle=array();
		$sfstyle=get_option('sfstyle');

		echo '<link rel="stylesheet" type="text/css" href="'.SF_PLUGIN_URL.'/styles/skins/'.$sfstyle['sfskin'].'/sf-credentials.css" />'."\n";
		echo '<script type="text/javascript" src="'.SF_PLUGIN_URL.'/credentials/sf-credentials.js"></script>'."\n";

		if(function_exists('site_url'))
		{
			$base  = trailingslashit(site_url());
		} else {
			$base = get_option('siteurl');
		}
		$forum = SFURL;
		
		?>
		<script type="text/javascript">
			window.onload=function(){
			sfjsetCredentials("<?php echo($base); ?>", "<?php echo($forum); ?>");
			}
		</script>
		<?php
	}
}

function sf_login_url()
{
	if(is_admin()) return;

	$sflogin=array();
	$sflogin=get_option('sflogin');
	if(!$sflogin['sfloginskin']) return;

	if(isset($_REQUEST['view'])) 
	{
		echo SFURL;
	}
}

function sf_login_title()
{
	if(is_admin()) return;

	$sflogin=array();
	$sflogin=get_option('sflogin');
	if(!$sflogin['sfloginskin']) return;

	if(isset($_REQUEST['view'])) 
	{
		echo get_option('blogname');
	}
}

function sf_login_form_action()
{
	if(is_admin()) return;

	$sflogin=array();
	$sflogin=get_option('sflogin');
	if(!$sflogin['sfloginskin']) return;

	if(isset($_REQUEST['view'])) 
	{ 
	?>
		<p class="submit"><input type="button" name="button1" value="<?php _e('Forum', "sforum"); ?>" onclick="sfjreDirect('<?php echo(SFURL); ?>');" /></p>
	<?php 
	}
}

function sf_register_as_forum()
{
	if(is_admin()) return;

	if(isset($_REQUEST['view']))
	{ 
		add_sfsetting($_SERVER['REMOTE_ADDR'], 'Registering');
	}
}

function sf_post_login_check($login_name)
{
	$dname = sf_get_login_display_name($login_name);
	if(function_exists('site_url'))
	{
		$base  = trailingslashit(site_url());
	} else {
		$base = get_option('siteurl');
	}

	$cookiepath = preg_replace('|https?://[^/]+|i', '', $base . '/' );
	setcookie('sforum_' . COOKIEHASH, stripslashes($dname), time() + 30000000, $cookiepath, false);

	delete_sfsetting($_SERVER['REMOTE_ADDR']);
}

function sf_get_login_display_name($login_name)
{
	global $wpdb;

	return $wpdb->get_var(
			"SELECT ".SFMEMBERS.".display_name 
			 FROM ".SFMEMBERS." 
			 JOIN ".SFUSERS." ON ".SFUSERS.".ID = ".SFMEMBERS.".user_id 
			 WHERE user_login = '".$login_name."';");
}

# = NEW USER EMAIL REPLACEMENT ================
if(!function_exists('wp_new_user_notification')):
function wp_new_user_notification($user_id, $user_pass='')
{
	$user = new WP_User($user_id);

	$eol = "\r\n";
	$message='';	

	$user_login = stripslashes($user->user_login);
	$user_email = stripslashes($user->user_email);

	$message .= sprintf(__('New user registration on your blog %s:', "sforum"), get_option('blogname')).$eol.$eol;
	$message .= sprintf(__('Username: %s', "sforum"), $user_login).$eol;
	$message .= sprintf(__('E-mail: %s', "sforum"), $user_email).$eol;
	$message .= sprintf(__('Registration IP: %s', "sforum"), $_SERVER['REMOTE_ADDR']).$eol;

	@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration', "sforum"), get_option('blogname')), $message);

	if ( empty($user_pass) )
		return;

	$mailoptions = get_option('sfnewusermail');
	$subject = stripslashes($mailoptions['sfnewusersubject']);
	$body = stripslashes($mailoptions['sfnewusertext']);
	if((empty($subject)) || (empty($body)))
	{
		$subject = sprintf(__('[%s] Your username and password', "sforum"), get_option('blogname')).$eol.$eol;
		$body = sprintf(__('Username: %s', "sforum"), $user_login).$eol;
		$body.= sprintf(__('Password: %s', "sforum"), $user_pass).$eol.$eol;
		$body.= SFLOGINEMAIL.$eol;
	} else {
		$blogname = get_bloginfo('name');
		$subject = str_replace('%USERNAME%', $user_login, $subject);
		$subject = str_replace('%PASSWORD%', $user_pass, $subject);
		$subject = str_replace('%BLOGNAME%', $blogname, $subject);
		$subject = str_replace('%SITEURL%', SFURL, $subject);
		$subject = str_replace('%LOGINURL%', SFLOGINEMAIL, $subject);
		$body = str_replace('%USERNAME%', $user_login, $body);
		$body = str_replace('%PASSWORD%', $user_pass, $body);
		$body = str_replace('%BLOGNAME%', $blogname, $body);
		$body = str_replace('%SITEURL%', SFURL, $body);
		$body = str_replace('%LOGINURL%', SFLOGINEMAIL, $body);
		$body = str_replace('%NEWLINE%', $eol, $body);
	}
	str_replace('<br />', $eol, $body);
	
	wp_mail($user_email, $subject, $body);
	return;
}
endif;

?>